/*
 * HttpHelper.h
 *
 */

#ifndef HTTPHELPER_H_
#define HTTPHELPER_H_

#include <string>
#include <vector>

#include "HttpRequest.h"
#include "HttpResponse.h"
#include "Hop.h"

using namespace std;

class HttpHelper {
public:
	HttpHelper();

	virtual ~HttpHelper();

	static int tcp_connect(const char *host, const char *serv);

	static void retrieveURIContent(HttpRequest request, HttpResponse & response);

	//put the data read from fd to HTTP server
	static void putURIContent(HttpRequest httpRequest, HttpResponse & response, int fd);

	//given an HTTP URL, extract the host part
	static string extractHost(string url);

	//given an HTTP URL, extract the port
	static int extractPort(string url);

	//given an HTTP URL, extract the resource path part from this URL
	static string extractPath(string url);

	static string createHttpRequestStr(HttpRequest httpRequest);

	// extract the HTTP request information from a received text msg
	static HttpRequest * parseHttpRequest(string msg);

	// extract the HTTP response from a received text msg
	static HttpResponse * parseHttpResponse(string msg);

	// extract the method from request line
	static string getMethodFromRequestLine(string& requestLine);

	// extract the path from the request line
	static string getRequestUriFromRequestLine(string& requestLine);

	static bool isValidMethod(string& methodName);

	/**
	 * Given the httpResponseHeader part,
	 * return the value of "content-length" header if it is there.
	 * return -1 if it is not there
	 */
	static int getContentLength(string httpResponseHeaders);

	static void cancelProcessing();	// cancel the processing and exit

	// push the http message through the clientSocket. the content of the http message is read from fd
	// return -1 if failed. 0 if succeed
	static int pushHttpMsg(string httpMsgHeaders, int clientSocket, int fd);

	// the name of the file for the downloaded content
    static const char *DOWNLOADED_FILE_NAME;

    // the file handle to the downloaded content file
    static FILE * DownloadFilefp;

    static bool endsWithSeparator(char * buffer);

    static int readHttpHeaders(int connfd, char * buffer, int buffSize);

    static HttpResponse * readHttpResponse(int connfd);

    static int connectToHop(Hop * nextHop);

    static bool isDirectoryListing(HttpRequest & request);

};

#endif /* HTTPHELPER_H_ */
